package com.test.mytest.saolei.swing;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;



public class SaoleiSwingApp {
	int width=8;
	int height=8;
	int lei=8;
	int boomLeftSize=lei;
	Map<BoomLabel, Integer> mapLeiInfo=new HashMap<>();
	BoomLabel[][] labels=null;

	private JFrame frame;
	private JTextField txt_width;
	private JTextField txt_height;
	private JTextField txt_leinum;
	private ShowGifPanel panel_main;
	private JLabel lbl_leftBoom;
	private JLabel lbl_time;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					SaoleiSwingApp window = new SaoleiSwingApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public SaoleiSwingApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 446, 388);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		
		txt_width = new JTextField();
		txt_width.setText("999");
		panel.add(txt_width);
		txt_width.setColumns(4);
		
		JLabel lblX = new JLabel("x");
		panel.add(lblX);
		
		txt_height = new JTextField();
		txt_height.setText("999");
		panel.add(txt_height);
		txt_height.setColumns(4);
		
		JLabel label_3 = new JLabel("雷数:");
		panel.add(label_3);
		
		txt_leinum = new JTextField();
		panel.add(txt_leinum);
		txt_leinum.setColumns(4);
		
		JButton button = new JButton("→");
		panel.add(button);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
		
		JLabel label = new JLabel("剩余雷数:");
		panel_1.add(label);
		
		lbl_leftBoom = new JLabel("999");
		panel_1.add(lbl_leftBoom);
		
		JLabel label_5 = new JLabel("时间:");
		panel_1.add(label_5);
		
		lbl_time = new JLabel("9999");
		panel_1.add(lbl_time);
		
		panel_main = new ShowGifPanel();
		frame.getContentPane().add(panel_main, BorderLayout.CENTER);
		panel_main.setLayout(null);
		
		initLei();
	}
	
	/**
	 * 
	 */
	public void initLei() {
		labels=new BoomLabel[width][height];
		for (int i = 0; i < height; i++) {
			for (int j = 0; j <width; j++) {
				BoomLabel label_4 = new BoomLabel("");
				label_4.setBorder(BorderFactory.createRaisedBevelBorder());
				label_4.setBackground(Color.GRAY);
				label_4.setBounds(j*30,i*30, 30, 30);
				label_4.setVerticalAlignment(JLabel.CENTER);
				label_4.setHorizontalAlignment(JLabel.CENTER);
				label_4.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseReleased(MouseEvent e) {
						System.out.println(label_4);
						int button2 = e.getButton();
						if (label_4.isOpen()) {
							int clickCount = e.getClickCount();
							if (clickCount==2) {
								List<BoomLabel> list = getAroundLabel(label_4.getwSize(), label_4.gethSize());
								
								int willBoomSize=0;
								int noOpenSize=0;
								for (BoomLabel boomLabel : list) {
									if (boomLabel.isWillBoom()) {
										willBoomSize++;
									}
									if (!boomLabel.isOpen()) {
										noOpenSize++;
									}
								}
								if (noOpenSize==0) {
									return;
								}
								if (label_4.getBoomSize()<=willBoomSize) {
									for (BoomLabel boomLabel2 : list) {
										if (!boomLabel2.isWillBoom()) {
											open(boomLabel2);
										}
									}
								}
							}
						}else{
    						if (button2==1) {
    							open(label_4);
    						}else{
    							if (label_4.isWillBoom()) {
    								label_4.setWillBoom(false);
        							boomLeftSize++;
								}else{
        							label_4.setWillBoom(true);
        							boomLeftSize--;
								}
    						}
						}
					}
				});
				label_4.setwSize(j);
				label_4.sethSize(i);
				labels[j][i]=label_4;
				mapLeiInfo.put(label_4, 0);
				panel_main.add(label_4);
			}
		}
		for (int i = 0; i < lei;) {
			Random r=new Random();
			int w = r.nextInt(width);
			int h = r.nextInt(height);
			BoomLabel e = labels[w][h];
			if (e.getBoomSize()>11) {
				continue;
			}
			e.setBoomSize(99);
			i++;
		}
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				BoomLabel key = labels[j][i];
				if (key.getBoomSize()>10) {
					continue;
				}
				List<BoomLabel> list = getAroundLabel(j,i);
				System.out.println("list.size()=="+list.size());
				int boomNum=0;
				for (BoomLabel jLabel : list) {
					if (jLabel.getBoomSize()>10) {
						boomNum++;
					}
				}
				key.setBoomSize(boomNum);
			}
		}
		frame.setSize(width*30+15, height*30+110);
		for (BoomLabel boomLabel : mapLeiInfo.keySet()) {
			System.out.println(boomLabel);
		}
		long currentTimeMillis = System.currentTimeMillis();
		ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
		executorService.scheduleWithFixedDelay(new Runnable() {
			@Override
			public void run() {
				lbl_leftBoom.setText(boomLeftSize+"");
				lbl_time.setText((System.currentTimeMillis()-currentTimeMillis)/1000+"");
			}
		}, 1, 1, TimeUnit.SECONDS);
		panel_main.setBackgroudImage("D:\\test.gif");
	}

	protected void open(BoomLabel label) {
		System.out.println("open:"+label.getwSize()+"=="+label.gethSize());
		label.setBorder(BorderFactory.createEtchedBorder());
		if (label.isOpen()) {
			return;
		}
		int boomSize = label.getBoomSize();
		if (boomSize==0) {
			openNoBoom(label);
		}else if(boomSize<10){
			label.open();
		}else{
			for (BoomLabel boomLabel : mapLeiInfo.keySet()) {
				boomLabel.open();
				boomLabel.setBorder(null);
			}
		}
		
	}

	private void openNoBoom(BoomLabel label) {
		label.open();
		List<BoomLabel> list = getAroundLabel(label.getwSize(), label.gethSize());
		for (BoomLabel boomLabel : list) {
			if (boomLabel.isOpen()) {
				continue;
			}
			System.out.println(boomLabel);
			int boomSize = boomLabel.getBoomSize();
			if (boomSize==0) {
				openNoBoom(boomLabel);
			}else{
				boomLabel.open();
			}
		}
	}

	private List<BoomLabel> getAroundLabel(int w, int h) {
		List<BoomLabel> list=new ArrayList<>();
		int wn=0;
		int hn=0;
		for (int i = -1; i <=1; i++) {
			for (int j = -1; j <= 1; j++) {
				if (i==0&&j==0) {
					continue;
				}
				wn=w+i;
				hn=h+j;
				System.out.println(w+"=="+h+"::"+wn+"=="+hn);
				if (wn<0||hn<0||wn>=width||hn>=height) {
					continue;
				}
				list.add(labels[wn][hn]);
			}
		}
		return list;
	}
}
